import * as echarts from '../../lib/ec-canvas/echarts';
const app = getApp();

function initChart(canvas, width, height) {
  const chart = echarts.init(canvas, null, {
    width: width,
    height: height
  });
  canvas.setChart(chart);
  var option = option = {
    tooltip: {
      show: true,
      formatter: "{a} <br/>{b} : {c} ({d}%)"
    },
    toolbox: {
      show: true,
      feature: {
        mark: {
          show: true
        },
        dataView: {
          show: true,
          readOnly: false
        },
        restore: {
          show: true
        },
        saveAsImage: {
          show: true
        }
      }
    },
    color: ['#56cbff', '#ff6300'],
    calculable: true,
    series: [{
      name: '分类',
      type: 'pie',
      center: ['50%', '50%'],
      radius: 80,
      itemStyle: {
        normal: {
          label: {
            show: true,
            position: 'inner',
            formatter: function(params) {
              return (params.percent - 0).toFixed(0) + '%'
            }
          },
          labelLine: {
            show: false
          }
        },
        emphasis: {
          label: {
            show: true,
            formatter: "{b}\n{d}%"
          }
        }
      },
      data: [{
          value: 20,
          name: '使用'
        },
        {
          value: 80,
          name: '未使用'
        }
      ]
    }]
  };
  chart.setOption(option);
  return chart;
}

Page({

  data: {
    value: '2020-04-14',
    week: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
    lastMonth: 'lastMonth',
    nextMonth: 'nextMonth',
    selectVal: '',
    ec: {
      onInit: initChart,
      // lazyLoad: true
    }
  },
  init_one: function(X, Y) { //初始化第一个图表
    this.oneComponent.init((canvas, width, height) => {
      const chart = echarts.init(canvas, null, {
        width: width,
        height: height
      });
      var option = option = {
        tooltip: {
          show: true,
          formatter: "{a} <br/>{b} : {c} ({d}%)"
        },
        toolbox: {
          show: true,
          feature: {
            mark: {
              show: true
            },
            dataView: {
              show: true,
              readOnly: false
            },
            restore: {
              show: true
            },
            saveAsImage: {
              show: true
            }
          }
        },
        color: ['#56cbff', '#ff6300'],
        calculable: true,
        series: [{
          name: '分类',
          type: 'pie',
          center: ['50%', '50%'],
          radius: 80,
          itemStyle: {
            normal: {
              label: {
                show: true,
                position: 'inner',
                formatter: function(params) {
                  return (params.percent - 0).toFixed(0) + '%'
                }
              },
              labelLine: {
                show: false
              }
            },
            emphasis: {
              label: {
                show: true,
                formatter: "{b}\n{d}%"
              }
            }
          },
          data: [{
              value: X,
              name: '使用'
            },
            {
              value: Y,
              name: '未使用'
            }
          ]
        }]
      };
      chart.setOption(option)
      this.chart = chart;
      return chart;
    });
  },
  select(e) {
    // console.log(e.detail)
    this.setData({
      selectVal: e.detail
    })
    this.getLabList()
    this.getAnalyze()
    
    var that = this
    setTimeout(function () {
      var X = that.data.labList.length / that.data.labLength * 100
      var Y = 100 - X
      that.init_one(X, Y)
    }, 1000) //延迟时
   
  },

  toggleType() {
    this.selectComponent('#Calendar').toggleType();
  },

  getLabList() {
    var that = this
    wx.request({
      url: app.globalData.API + '/lab/borrow/list',
      data: {
        date: that.data.selectVal
      },
      method: "get",
      header: {
        'cookie': wx.getStorageSync("sessionid"), //读取cookie
        "content-type": "application/json"
      },
      success: function(res) {
        console.log("[subscribe.js] /lab/borrow/list 请求成功", res)
        if (res.data.code == 200) {
          that.setData({
            labList: res.data.data
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
          })
        }
      },
      fail: res => {
        console.log("[subscribe.js] /lab/borrow/list 请求失败", res.data)
      },
      complete: res => {

      }
    })
  },
  onLoad: function() {
    var that = this
    this.piechartsComponnet = this.selectComponent('#mychart-dom-line'); //饼图
    this.getLabLista()
    // timer: setInterval(function () {
    //   that.init_one();
    // }, 1000)
  },
  onReady: function() { //这一步是一定要注意的
    this.oneComponent = this.selectComponent('#mychart-dom-line');
  },
  getAnalyze() {
    var that = this
    wx.request({
      url: app.globalData.API + '/lab/analyze',
      data: {
        date: that.data.selectVal
      },
      method: "get",
      header: {
        'cookie': wx.getStorageSync("sessionid"), //读取cookie
        "content-type": "application/json"
      },
      success: function(res) {
        console.log("[subscribe.js] /lab/analyze 请求成功", res)
        if (res.data.code == 200) {
          that.setData({
            analyze: res.data.data
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
          })
        }
      },
      fail: res => {
        console.log("[subscribe.js] /lab/analyze 请求失败", res.data)
      },
      complete: res => {

      }
    })
  },
  //初始化图表--饼图
  init_pieCharts: function() {
    this.piechartsComponnet.init((canvas, width, height) => {
      // 初始化图表
      const pieChart = echarts.init(canvas, null, {
        width: width,
        height: height
      });
      pieChart.setOption(this.getPieOption());
      // 注意这里一定要返回 chart 实例，否则会影响事件处理等
      return pieChart;
    });
  },


  getLabLista() {
    var that = this
    wx.request({
      url: app.globalData.API + '/lab/borrow/list',
      data: {
        // examine_status: 0
      },
      method: "get",
      header: {
        'cookie': wx.getStorageSync("sessionid"), //读取cookie
        "content-type": "application/json"
      },
      success: function(res) {
        console.log("[subscribe.js] /lab/borrow/list 请求成功", res)
        if (res.data.code == 200) {
          that.setData({
            labLength: res.data.data.length
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
          })
        }
      },
      fail: res => {
        console.log("[subscribe.js] /lab/borrow/list 请求失败", res.data)
      },
      complete: res => {

      }
    })
  },

})